#include <stdio.h>
#include <stdlib.h>

typedef struct node *Nodeptr;
typedef struct node {
  int i;
  Nodeptr next;
} Node;

void reverse_3ptr(Nodeptr *head)
{
  Nodeptr pre = NULL;
  Nodeptr curr = *head;
  Nodeptr succ;
  while(curr != NULL) { //ganrantee curr has a next
    succ = curr->next;
    curr->next = pre;
    pre = curr;
    curr = succ;
  }
  *head = pre;
}
void xor_swap(Nodeptr* a,Nodeptr* b)
{
  (*a) =(Nodeptr*)((int)(*a)^(int)(*b));
  (*b) =(Nodeptr*)((int)(*a)^(int)(*b));
  (*a) =(Nodeptr*)((int)(*a)^(int)(*b));
}
void reverse_2ptr(Nodeptr *head)
{
  Nodeptr pred = NULL;
  Nodeptr curr= *head;
  while(curr != NULL) {
    xor_swap(&curr->next,&pred);
    xor_swap(&curr,&pred);
  }
  *head = pred;
}
void push_front(Nodeptr *head,int i)
{
  Nodeptr tmp = (Nodeptr)malloc(sizeof(Node) );
  tmp->i = i;
  tmp->next = *head;
  *head = tmp;
  return;
}

void print(Nodeptr *head)
{
  Nodeptr iter = *head;
  while(iter != NULL) {
    printf("%d\t",iter->i);
    iter = iter->next;
  }
  puts("");
}
int clear(Nodeptr *head)
{
  Nodeptr tmp;
  while(*head != NULL) {
    tmp = *head;
    *head = (*head)->next;
    printf("%d has been deleted...\n",tmp->i);
    free(tmp);
  }
  return 1;
}
int main(int argc, char *argv[])
{
  int i;
  Nodeptr head = NULL;
  //build singly-linked-list
  for(i=argc-1;i>=1;--i){
    push_front(&head,atoi(argv[i]));
  }

  

  print(&head);
  //op
  reverse_2ptr(&head);
  print(&head);

  clear(&head);

  print(&head);
  return 0;
}
